﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Drawing.Imaging;

public partial class frmDrugsList : BaseWinFrom
{
    public frmDrugsList()
    {
        InitializeComponent();
        DataTable dt = new Drugs().List(null);

        MemoryStream ms;

        dgvDrugsList.AutoGenerateColumns = false;
        ms = new MemoryStream();
        global::HealthExa.Properties.Resources.Edit.Save(ms, ImageFormat.Bmp);
        dt.Columns.Add(new DataColumn() { ColumnName = "Cmd", DataType = typeof(byte[]), DefaultValue = ms.ToArray() });

        global::HealthExa.Properties.Resources.Delete.Save(ms, ImageFormat.Bmp);
        dt.Columns.Add(new DataColumn() { ColumnName = "Delete", DataType = typeof(byte[]), DefaultValue = ms.ToArray() });

        this.dgvDrugsList.DataSource = dt;
    }

    private void dgvDrugsList_CellClick(object sender, DataGridViewCellEventArgs e)
    {
        DataGridView dgv = sender as DataGridView;
        DataTable dt = dgv.DataSource as DataTable;

        switch (e.ColumnIndex)
        {
            case 5:
                if (e.RowIndex >= dt.Rows.Count)
                {
                    DataRow dr = dt.NewRow();
                    foreach (DataColumn i in dt.Columns)
                    {
                        foreach (DataGridViewColumn j in dgv.Columns)
                        {
                            if (i.ColumnName == j.Name)
                            {
                                dr[i] = dgv.Rows[e.RowIndex].Cells[j.Name].Value;
                                break;
                            }
                        }
                    }
                    new Drugs().Change(dr);
                    dgv.Rows.RemoveAt(e.RowIndex);
                    dt.Rows.Add(dr);
                }
                else
                {
                    new Drugs().Change(dt.Rows[e.RowIndex]);
                }
                break;//Edit;
            case 6:
                if (e.RowIndex < dt.Rows.Count)
                {
                    object Id = dgv.Rows[e.RowIndex].Cells["Id"].Value;
                    if (Id == DBNull.Value)
                        return;
                    new Drugs() { Id = Id.ToString() }.Delete();
                    foreach (DataRow i in dt.Rows)
                    {
                        if (i["Id"].ToString() == Id.ToString())
                        {
                            dt.Rows.Remove(i);
                            break;
                        }
                    }
                    //dt.Rows.RemoveAt();
                }
                break;//Delete;
        }
    }
}